package com.aistar.config;

import com.aistar.pojo.Travelnote;
import com.aistar.service.es_search.ES_SearchService;
import com.aistar.service.es_search.impl.TravelnoteRepository;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

/**
 * @author shkstart
 * @date 2019/10/16 - 14:32
 */
@Slf4j
public class ESTask extends QuartzJobBean {

    @Qualifier("TravelnoteES_SearchServiceImpl")
    @Autowired
    private ES_SearchService travelnoteService;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    TravelnoteRepository travelnoteRepository;


    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<Travelnote> travelnotes = travelnoteService.queryAllResults();
        if (null != travelnotes && travelnotes.size() != 0) {
            if (elasticsearchTemplate.indexExists("Travelnote")) {
                elasticsearchTemplate.deleteIndex(Travelnote.class);
            }
            elasticsearchTemplate.createIndex(Travelnote.class);
            elasticsearchTemplate.putMapping(Travelnote.class);
            travelnoteRepository.saveAll(travelnotes);
            System.out.println("创建店家菜品收藏elasticsearch索引成功》》》》》》》》》》》》》》》》》》》");
        } else {
            if (elasticsearchTemplate.indexExists("Travelnote")) {
                elasticsearchTemplate.deleteIndex(Travelnote.class);
            }
            elasticsearchTemplate.createIndex(Travelnote.class);
            elasticsearchTemplate.putMapping(Travelnote.class);
            System.out.println("没有店家菜品收藏信息>>>>>>>>>>>>>>>>>>>>>>>");
        }
    }
}
